<?php
// Russell <version>
// Russell's original source code is (c) David Kendal
//---------------------------------------------------
// This work 'as-is' we provide.
// No warranty express or implied.
// We've done our best,
// to debug and test.
// Liability for damages denied.
//
// Permission is granted hereby,
// to copy, share, and modify.
// Use as is fit,
// free or for profit.
// These rights, on this notice, rely.
//---------------------------------------------------

// first, define some versioning tools
// (if you checked out from SVN the version will be <version>
// this is used in the compile farm to indicate where to sub in the version)
// ([if you like, replace it with the revision number])
define(KERNEL, "Russell");
define(RUSSELL_VER, "<version>");
define(IS_RUSSELL, TRUE);

// require the requireds.
require_once(dirname(__FILE__)."/rss.php");
require_once(dirname(__FILE__)."/push.php");
require_once(dirname(__FILE__)."/db.php");

// if we're being called to push, so do. verily.
if (isset($_GET['push'])) {
  // nothing here yet.
}

class Russell {
  public $rss;
  public $push;
  public $db;
  protected $feedconf; // the Database->feed mapping
  
  // constructor.
  // creates instances of each module and shifts it to $this
  public function __construct() {
    $this->rss = new RSS;
    $this->push = new Push;
    $this->db = $db;
  }
  
  // loads in an array for handling a table and feeding it to the RSS generator.
  // $table is the table this applies to, $config is an array ($entry_in_rss => $row_in_db)
  public function db_feed_map($table, $config) {
    $this->feedconf[$table] = $config;
  }
  
  // generates a feed based on the table and feed information provided
  // this IS the db->feedgen pipe
  // this function assumes that the feed will be the only content of the page.
  public function generate_feed($table, $info) {
    if (!isset($this->feedconf[$table])) {
      // no info? sorry.
      trigger_error("Russell couldn't find a Database-to-Feed mapping for the table $table .", E_USER_ERROR);
    }
    $fconf = $this->feedconf[$table];
    $this->db->query("SELECT * FROM ".$table." WHERE 1");
    $this->rss->set_feed_info($info);
    if ($row = $this->db->fetch_array($result)) {
      while($row = $this->db->fetch_array($result)) {
        // long but understandable (hopefully)
        // checks each entry in the rss feed against the feed config
        // if it's there, it goes in the feed.
        if (isset($fconf['title'])) { 
          $this->rss->set_entry_title($row[$fconf['title']]);
        }
        if (isset($fconf['description'])) { 
          $this->rss->set_entry_title($row[$fconf['description']]);
        }
        if (isset($fconf['link'])) { 
          $this->rss->set_entry_title($row[$fconf['link']]);
        }
        if (isset($fconf['time'])) { 
          $this->rss->set_entry_title($row[$fconf['time']]);
        }
        if (isset($fconf['id'])) { 
          $this->rss->set_entry_title($row[$fconf['id']]);
        }
      }
    } else {
      trigger_error("Russell couldn't find any information to map into this feed in the table $table .", E_USER_ERROR);
    }
  }
}
?>